package com.julang.check.utils;

import com.julang.check.bean.RepeatWord;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.*;

import java.util.Iterator;
import java.util.List;

import static com.julang.check.utils.CommonData.IGNORESUPPERCASE;
/**
 * @author 赵自强
 * */
public class WordReplaceUtil {

	public static void replaceFooterAndHeader(XWPFDocument doc, List<RepeatWord> params) {
		List<XWPFParagraph> footers = doc.getHeaderFooterPolicy().getDefaultFooter().getParagraphs();
		List<XWPFParagraph> headers = doc.getHeaderFooterPolicy().getDefaultHeader().getParagraphs();
		//处理页脚
		for (XWPFParagraph paragraph : footers) {
			List<XWPFRun> runs = paragraph.getRuns();
			for (XWPFRun run : runs) {
				String text = run.getText(0);
				if (StringUtils.isNotEmpty(text)) {
					for (int i = 0; i < params.size(); i++) {
						if (text.contains(params.get(i).getOrginWord()) && params.get(i).isCanPlace()) {
							text = text.replaceAll(IGNORESUPPERCASE + params.get(i).getOrginWord(), params.get(i).getReplaceWord());
							run.setText(text, 0);
						}
					}
				}
			}
		}
		//处理页眉
		for (XWPFParagraph paragraph : headers) {
			List<XWPFRun> runs = paragraph.getRuns();
			for (XWPFRun run : runs) {
				String text = run.getText(0);
				if (StringUtils.isNotEmpty(text)) {
					for (int i = 0; i < params.size(); i++) {
						if (text.contains(params.get(i).getOrginWord()) && params.get(i).isCanPlace()) {
							text = text.replaceAll(IGNORESUPPERCASE + params.get(i).getOrginWord(), params.get(i).getReplaceWord());
							run.setText(text, 0);
						}
					}
				}
			}
		}
	}


	public static XWPFDocument generateWord(List<RepeatWord> param, String filePath) {
		XWPFDocument doc = null;
		try {
			OPCPackage pack = POIXMLDocument.openPackage(filePath);
			doc = new XWPFDocument(pack);
			if (param != null && param.size() > 0) {
				//处理段落
				List<XWPFParagraph> paragraphList = doc.getParagraphs();
				processParagraphs(paragraphList, param, doc);
				//处理表格
				Iterator<XWPFTable> it = doc.getTablesIterator();
				while (it.hasNext()) {
					XWPFTable table = it.next();
					List<XWPFTableRow> rows = table.getRows();
					for (XWPFTableRow row : rows) {
						List<XWPFTableCell> cells = row.getTableCells();
						for (XWPFTableCell cell : cells) {
							List<XWPFParagraph> paragraphListTable = cell.getParagraphs();
							processParagraphs(paragraphListTable, param, doc);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("替换完成");
		return doc;
	}


	private static void processParagraphs(List<XWPFParagraph> paragraphList, List<RepeatWord> param,
	                                      XWPFDocument doc) {
		if (paragraphList != null && paragraphList.size() > 0) {

			for (XWPFParagraph paragraph : paragraphList) {
				List<XWPFRun> runs = paragraph.getRuns();
				doc.setParagraph(paragraph, 0);
				for (XWPFRun run : runs) {
					String text = run.getText(0);
					if (text != null) {
						for (int i = 0; i < param.size(); i++) {
							if (text.contains(param.get(i).getOrginWord()) && param.get(i).isCanPlace()) {
								text = text.replaceAll(IGNORESUPPERCASE + param.get(i).getOrginWord(),
										param.get(i).getReplaceWord());
								System.out.println(param.get(i).getOrginWord() + "  替 换 " + param.get(i).getReplaceWord());
								run.setText(text, 0);
							}
						}
						//      if(isSetText){
						//       run.setText(text,0);
						//      }
					}
				}
			}
		}
	}
}
